#include "types/common.h"

#ifndef _bpnode_h_
#define _bpnode_h_

struct index_data
{
	UINT32 key;
	RecordId id;
}__attribute__((__packed__ ));

const UINT32 KEY_SIZE = sizeof(UINT32);
const UINT32 PTR_SIZE = sizeof(UINT32);
const UINT32 DATA_SIZE = sizeof(index_data);

const UINT32 NUM_KEYS = (BLOCK_SIZE - 3 * PTR_SIZE)/ (KEY_SIZE + PTR_SIZE);  //Will evaluate to 4
const UINT32 NUM_PTRS =  1 + NUM_KEYS; //Will evaluate to 5
const UINT32 NUM_LEAF_DATA = (BLOCK_SIZE - (2*PTR_SIZE)) / DATA_SIZE; // 4 data

#define MASK_NO_KEY_OVERFLOW		0x00
#define MASK_FIRST_KEY_OVERFLOW		0x01
#define MASK_SECOND_KEY_OVERFLOW	0x02
#define MASK_THIRD_KEY_OVERFLOW		0x04
#define MASK_FOURTH_KEY_OVERFLOW	0x08


//Order of the B-tree
#define ORDER NUM_PTRS //5
#define BLKSIZE 48

struct index_entry
{
	BYTE LeafNode; //0 for intermediate node, 1 for Leaf Node
	BYTE keyCount; //Number of keys
	BYTE reserved[2];
	UINT32 left;
	UINT32 right;
	UINT32 key[NUM_KEYS];
	UINT32 child[NUM_PTRS];
}__attribute__((__packed__ ));

struct data_entry
{
	BYTE LeafNode; //0 for intermediate node, 1 for Leaf Node
	BYTE keyCount; //Number of keys
	BYTE ovwFlowMask; // Stores the Overflow/Duplicate Information
	BYTE reserved;
	UINT32 left;
	UINT32 right;
	struct index_data data[NUM_LEAF_DATA];
}__attribute__((__packed__ ));

struct index_header
{
	BYTE dupAllowed; //0 - FALSE, 1 - TRUE
	BYTE numLevels;
	UINT32 numBlocks;
	BYTE reserved[42];
}__attribute__((__packed__ ));

typedef struct
{
	BYTE cacheBlock[48];
	UINT32 blockNum;
}MemoryCache;

#endif
